<?php
/**
* phpCan - http://idc.anavallasuiza.com/
*
* phpCan is released under the GNU Affero GPL version 3
*
* More information at license.txt
*/

namespace templates\html;

defined('ANS') or die();

class Text_transform {
	private $Debug;
	private $settings = array();
	
	
	/**
	* public function __construct (void)
	*/
	public function __construct () {
		global $Debug;
		
		$this->Debug = $Debug;
		
		$this->settings['date'] = array(
			// Value array strings must content:
			// array(date_string[string], absolute_date[boolean])
			// First empty key value is for forced absolute dates
			'past_strings' => array(
				'' => array(__('on %l, %F %d, %Y'), true),
				31556926 => array(__('on %l, %F %d, %Y'), true),
				86400 => array(__('on %l, %F %d, %Y'), true),
				7200 => array(__('%h hours ago'), false),
				3600 => array(__('one hour ago'), false),
				120 => array(__('%i minutes ago'), false),
				60 => array(__('one minute ago'), false),
				0 => array(__('some seconds ago'), false)
			),
		
			'future_strings' => array(
				'' => array(__('on %l, %F %d, %Y'), true),
				31556926 => array(__('on %l, %F %d, %Y'), true),
				0 => array(__('on %l, %F %d, %Y'), true)
			)
		);

		krsort($this->settings['date']['past_strings']);
		krsort($this->settings['date']['future_strings']);
	}
	
	
	/**
	* public function tags (string $text)
	*
	* return string
	*/
	public function tags ($text) {
		$text = preg_replace_callback('/\[([a-z0-9,\-_\s]+)\]/i', array($this, 'checkTag'), $text);
		
		return $text;
	}


	/**
	* private function checkTag (array $match)
	*
	* return string
	*/
	private function checkTag ($match) {
		$options = explode(',', str_replace(' ', '', $match[1]));
		$method = 'tag_'.array_shift($options);
		$this->Debug->e($match);
		if (method_exists($this, $method)) {
			$ok = $this->$method($options);
			
			if ($ok !== false) {
				return $ok;
			}
		}
		
		return $match[0];
	}
	
	
	/**
	* private function tag_youtube (array $options)
	*
	* return false/string
	*/
	private function tag_youtube ($options) {
		$id = $options[0];
		$width = $options[1] ? intval($options[1]) : 435;
		$height = $options[2] ? intval($options[2]) : 350;
		
		return '<div class="youtube">
			<object width="'.$width.'" height="'.$height.'">
				<param name="movie" value="http://www.youtube.com/v/'.$id.'"></param>
				<param name="wmode" value="transparent"></param>
				<embed src="http://www.youtube.com/v/'.$id.'" type="application/x-shockwave-flash" wmode="transparent" width="'.$width.'" height="'.$height.'"></embed>
			</object>
		</div>';
	}
	
	
	/**
	* private function tag_vimeo (array $options)
	*
	* return false/string
	*/
	private function tag_vimeo ($options) {
		$id = $options[0];
		$width = $options[1] ? intval($options[1]) : 435;
		$height = $options[2] ? intval($options[2]) : 350;
		
		return '<div class="vimeo">
			<object width="'.$width.'" height="'.$height.'">
				<param name="allowfullscreen" value="true" />
				<param name="allowscriptaccess" value="always" />
				<param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id='.$id.'&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" />
				<embed src="http://vimeo.com/moogaloop.swf?clip_id='.$id.'&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="'.$width.'" height="'.$height.'"></embed>
			</object>
		</div>';
	}
	

	/**
	* public function date ($string, [boolean $strict])
	*
	* return string
	*/
	public function date ($string, $absolute = false, $time = false) {
		if (preg_match('/^[0-9]+$/', $string)) {
			$Date = new \DateTime;
			$Date->setTimeStamp($string);
		} else {
			$Date = new \DateTime($string);
		}

		$now = new \DateTime('now');

		$diff = $Date->diff($now);

		$new_format = '';

		if (is_string($absolute)) {
			$new_format = $absolute;
			$seconds = '';
		} else {
			$seconds = ($absolute) ? '' : $this->getSecondsFromDateObject($diff);
		}

		$date_strings = $this->settings['date'][ ($diff->invert === 0) ? 'past_strings' : 'future_strings' ];

		foreach ($date_strings as $string_time => $string_array) {
			list($string_value, $string_absolute) = $string_array;

			if (($seconds >= $string_time) && (gettype($seconds) == gettype($string_time))) {
				if ($new_format) {
					$string_value = $new_format;
				}

				$string_value = __($string_value);
				$string_value .= $time ? __(' at %H:%i') : '';

				if (!preg_match('/%[a-z]/i', $string_value)) {
					return $string_value;
				}

				preg_match_all('/%([a-z])/i', $string_value, $find);

				foreach ($find[1] as $find_value) {
					if (in_array($find_value, array('D', 'l', 'M', 'F'))) {
						switch ($find_value) {
							case 'D':
								$new_string = __('weekday_min_'.$Date->format('N'));
								break;
							case 'l':
								$new_string = __('weekday_'.$Date->format('N'));
								break;
							case 'M':
								$new_string = __('month_min_'.$Date->format('m'));
								break;
							case 'F':
								$new_string = __('month_'.$Date->format('m'));
								break;
						}

						$string_value = str_replace('%'.$find_value, $new_string, $string_value);
					} else {
						if ($string_absolute) {
							$string_value = str_replace('%'.$find_value, $Date->format($find_value), $string_value);
						} else {
							$string_value = str_replace('%'.$find_value, $diff->format('%'.$find_value), $string_value);
						}
					}
				}

				return $string_value;
			}
		}

		return $string;
	}

	public function getDiffFromDate ($string, $format) {
		if (preg_match('/^[0-9]+$/', $string)) {
			$Date = new DateTime;
			$Date->setTimeStamp($string);
		} else {
			$Date = new DateTime($string);
		}

		$now = new DateTime('now');

		$diff = $Date->diff($now);

		return $diff->$format;
	}

	public function getSecondsFromDateObject ($object) {
		return $object->days * 86400 + $object->h * 3600 + $object->i * 60 + $object->s;
	}
}
?>
